﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Creator.Utilitys
{
    
	public static class ExceptionOperater
	{

		public static string GetExceptionString(this Exception e)
		{
			return GetSaveStringFromException(string.Empty, e);
		}
		/// <summary>
		/// 解析错误信息
		/// </summary>
		/// <param name="text"></param>
		/// <param name="ex"></param>
		/// <returns></returns>
		public static string GetSaveStringFromException(string text, Exception ex)
		{
			StringBuilder builder = new StringBuilder(text);

			if (ex != null)
			{
				if (!string.IsNullOrEmpty(text))
				{
					builder.Append(" : ");
				}

				try
				{
					builder.Append("错误信息：");
					builder.Append(ex.Message);
					builder.Append(Environment.NewLine);
					builder.Append("错误源:");
					builder.Append(ex.Source);
					builder.Append(Environment.NewLine);
					builder.Append("错误栈区:");
					builder.Append(ex.StackTrace);
					builder.Append(Environment.NewLine);
					builder.Append("错误类型：");
					builder.Append(ex.GetType().ToString());
					builder.Append(Environment.NewLine);
					builder.Append("错误方法");
					builder.Append(ex.TargetSite?.ToString());
				}
				catch
				{

				}
				builder.Append(Environment.NewLine);
				builder.Append("\u0002/=================================================[    Exception    ]================================================/");
			}

			return builder.ToString();
		}


	}
		 
	 
 
}
